﻿using DadayunApi.Model;
using KhtApi.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace KhtApi.DAL
{
    internal class ReportDAL:KhtDAL
    {
        public ReportDAL(AppAuthBase authBase, string baseUri) : base(authBase, baseUri)
        {//继承KhtDal，拿到reportApi

        }

        public List<StockBalance>GetStockBalances(IEnumerable<ReportFilter>filter)
        {
            var ans = GetReport<StockBalanceReport>("库存余额查询", filter);
            if (ans != null)
                return ans.Results;
            else return new List<StockBalance>();
        }

        /// <summary>
        /// 查询订单执行看板
        /// </summary>
        /// <param name="filter">支持的查询列：销售订单号|销售代表|客户名称|产品编码|产品名称|型号|发货情况|计划情况</param>
        /// <returns></returns>
        public List<SoBillBoard> GetSoBillBoard(IEnumerable<ReportFilter> filter)
        {
            var ans= GetReport<SoBillBoardReport>("销售订单执行进度查询", filter);
            if (ans != null)
                return ans.Results;
            else return new List<SoBillBoard>();
        }

        /// <summary>
        ///  查询产品信息
        /// </summary>
        /// <returns></returns>
        public List<ProductRepTpl>GetProductReport(IEnumerable<ReportFilter> filter)
        {
            var ans = GetReport<ProductReport>("产品信息查询", filter);
            if (ans != null)
                return ans.Results;
            else return new List<ProductRepTpl>();
        }
        /// <summary>
        ///  查询BOM报表
        /// </summary>
        /// <returns></returns>
        public List<BomReportTpl> GetBomReport(IEnumerable<ReportFilter> filter)
        {
            var ans = GetReport<BomReport>("产品BOM查询", filter);
            if (ans != null)
                return ans.Results;
            else return new List<BomReportTpl>();
        }

        /// <summary>
        /// BOM反查：查找使用了某物料的所有BOM
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public BomPeggingReport BomPegging(IEnumerable<ReportFilter> filter)
        {
            return GetReport<BomPeggingReport>("BOM反查", filter);
        }

        private T GetReport<T> (string reportName, IEnumerable<ReportFilter> filter)
        {
            var ans = reportApi.GetReportInJson(reportName, out int totalCount, EReportKeyOptions.ColName, filter, 0, 100000);
            T res = JsonConvert.DeserializeObject<T>(ans);

            return res;
        }

       
    }
}
